//
//  ZYHomeViewController.swift
//  WBBeta1
//
//  Created by wustzhy on 16/8/9.
//  Copyright © 2016年 Yestin. All rights reserved.
//

import UIKit
//cell重用标示
private let HMHomeViewCellId = "HMHomeViewCellId"

class ZYHomeViewController: ZYVisitorViewController {
    
    private lazy var statusListViewModel:ZYStatusListViewModel = ZYStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !isLogin {
            //未登录
            visitorView?.setupVisitorViewInfo(nil , imgName: nil)
            return
        }
        setupTableView()
        //登录后
        loadData()
        
    }
    
    func setupTableView(){
        tableview.dataSource = self
        tableview.delegate = self
        tableview.registerClass(ZYStatusCell.self, forCellReuseIdentifier: HMHomeViewCellId)
//        tableview.rowHeight = 200
        tableview.estimatedRowHeight = 200
        tableview.rowHeight = UITableViewAutomaticDimension
        //下拉刷新--菊花添加
        tableview.tableFooterView = activityView
        //上拉刷新--
        tableview.addSubview(refreshControl)
        
        //tableview分割线设置 无
        tableview.separatorStyle = .None
    }
    
    func refreshActioin(){
        loadData()
        //数据请求完后 关闭菊花
    }
    
    //菊花-上拉
    private lazy var activityView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        view.color = AppThemeColor
        return view
    }()
    //菊花--下拉
    private lazy var refreshControl:HMRefreshControl = {
//        let refreshControl = UIRefreshControl()
        //刷新控件
        let refreshControl = HMRefreshControl()
        refreshControl.addTarget(self, action: "refreshActioin", forControlEvents: UIControlEvents.ValueChanged)
        return refreshControl
    }()
}

//首页数据展示
extension ZYHomeViewController: UITableViewDataSource,UITableViewDelegate{
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return statusListViewModel.dataArray.count
//        return 20
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableview.dequeueReusableCellWithIdentifier(HMHomeViewCellId, forIndexPath: indexPath) as! ZYStatusCell
//        cell.textLabel?.text = "\(indexPath.row)"
        cell.statusViewModel = statusListViewModel.dataArray[indexPath.row]
        return cell
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row + 1 == statusListViewModel.dataArray.count && !activityView.isAnimating() {
            print(indexPath.row)
            //菊花动画
            activityView.startAnimating()
            //请求 数组
            loadData()
            //请求完毕，关闭菊花 在loadData中执行
        }
    }
    
}

//
extension ZYHomeViewController {
    
    func loadData() {

        statusListViewModel.loadData(activityView.isAnimating()) { (isSuccess) -> () in
            if !isSuccess {
//                print("失败也要刷新")
                return
            }
            //print(self.statusListViewModel.dataArray)
            //刷新
            self.tableview.reloadData()
            self.endRefreshing()
        }
        
    }
    
    func endRefreshing(){
        //拿不拿到数据，都不用转了
        self.activityView.stopAnimating()
        self.refreshControl.endRefreshing()
    }
    
}






/*
----------------
娶妻娶德不娶色
----------------
嫁人嫁心不嫁财
----------------
交友交心不交利
----------------
*/




//转移至 viewModel
////首页数据 下载
//extension ZYHomeViewController{
//    func loadData(){
//        
//        ZYNetworkTools.sharedTools.homeLoadData({ (response) -> Void in
//            guard let res = response?["statuses"] as? [[String:AnyObject]] else{
//                return
//            }
//            print(res)//获取的是字典?数组？
//
//            var tempArr:[ZYStatusModel] = [ZYStatusModel]()
//            for dic in res{
//                let mdl = ZYStatusModel(dic: dic)
//                tempArr.append(mdl)
//            }
//            print(tempArr.count)
////            self.dataArray = tempArr
//            //刷新UI
//
//            }) { (error) -> Void in
//            print("请求home数据失败")
//        }
//        
//    }
//    
//}




